package znbkTestPaperRdtz.saveRdtzPaper;

import android.app.Activity;
import android.os.Handler;

import com.example.lancooznbk.view.base.ILoadPaperView;
import com.lancoo.reviewsystem.utils.ToastUtils;
import com.lancoo.rxretrofitlibrary.Api.UserInfo;
import com.lancoo.rxretrofitlibrary.http.HttpManager;
import com.lancoo.rxretrofitlibrary.listener.HttpOnNextListener;

import znbkListRdtz.IHeatPointExtensionPresenter;
import znbkListXtfx.ISystemReviewPresenter;
import znbkTestPaperInfor.ZnbkPaperDef;
import znbkTestPaperRdtz.ZnbkPaperRdtzInfor;
import znbkTestPaperRdtz.activity.HeatPointExtensionPaperActivity;
import znbkTestPaperRdtz.activity.HeatPointExtensionPaperSplActivity;

/**
 * Created by Administrator on 2018\4\24 0024.
 */

public class SaveHeatPointExtensionPaperPresenter implements IHeatPointExtensionPresenter {

    private ILoadPaperView view;
    private int ModelFrom;
    private boolean isFromExit = false;
    private boolean isAssess = false;

    public SaveHeatPointExtensionPaperPresenter(ILoadPaperView view, int modelFrom) {
        this.view = (ILoadPaperView) view;
        this.ModelFrom = modelFrom;
    }

    @Override
    public void getData(boolean isShowLoading) {
        //  if (isShowLoading) view.showLoading();
        HttpOnNextListener<String> httpOnNextListener = new HttpOnNextListener<String>() {

            @Override
            public void onNext(String s) {

                if(s.equals("true")){
//                    ToastUtils.showShort("保存成功");
                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            ((Activity)view).finish();
                        }
                    },600);
                }else{
                    view.updatingResult(false);
//                    ToastUtils.showShort("学习失败! 错误码"+s.toString());
                }

            }

            @Override
            public void onError(Throwable e) {
                super.onError(e);
                //view.showNetError();
//                ToastUtils.showShort("学习失败!");
            }
        };

        SaveHeatPointExtensionPaperApi api = null;
        if(ModelFrom== ZnbkPaperDef.FROM_HEAT_POINT_EXTENSION) {
             api = new SaveHeatPointExtensionPaperApi(httpOnNextListener, (HeatPointExtensionPaperActivity) view);
        }else if(ModelFrom==ZnbkPaperDef.FROM_HEAT_POINT_EXTENSION_SPL){
            api = new SaveHeatPointExtensionPaperApi(httpOnNextListener, (HeatPointExtensionPaperSplActivity) view);
        }

        SaveHeatPointExtensionPaperEntity.ResModelBean resModelBean =
        new SaveHeatPointExtensionPaperEntity.ResModelBean(ZnbkPaperRdtzInfor.CreatTime, UserInfo.SubjectId,
                ZnbkPaperRdtzInfor.HotType,ZnbkPaperRdtzInfor.ResID,ZnbkPaperRdtzInfor.ResType,ZnbkPaperRdtzInfor.Title,
                UserInfo.StageNo,UserInfo.StuId,ZnbkPaperRdtzInfor.RecommendNumberOfTimes);

        String ZSDCodeAll = "";

        if(ModelFrom== ZnbkPaperDef.FROM_HEAT_POINT_EXTENSION) {
            for (int i = 0; i < HeatPointExtensionPaperActivity.mEntityRdtz.getResFromTK().getImportantZSD().size(); i++) {
                if (i == 0) {
                    ZSDCodeAll = HeatPointExtensionPaperActivity.mEntityRdtz.getResFromTK().getImportantZSD().get(0).getZSDCode();
                } else {
                    ZSDCodeAll = ZSDCodeAll + "|" + HeatPointExtensionPaperActivity.mEntityRdtz.getResFromTK().getImportantZSD().get(i).getZSDCode();
                }
            }
        }else if(ModelFrom==ZnbkPaperDef.FROM_HEAT_POINT_EXTENSION_SPL){
            for (int i = 0; i < HeatPointExtensionPaperSplActivity.mEntityRdtz.getResFromTK().getImportantZSD().size(); i++) {
                if (i == 0) {
                    ZSDCodeAll = HeatPointExtensionPaperSplActivity.mEntityRdtz.getResFromTK().getImportantZSD().get(0).getZSDCode();
                } else {
                    ZSDCodeAll = ZSDCodeAll + "|" + HeatPointExtensionPaperSplActivity.mEntityRdtz.getResFromTK().getImportantZSD().get(i).getZSDCode();
                }
            }
        }
        SaveHeatPointExtensionPaperEntity.ResPostBean resPostBean =
                new SaveHeatPointExtensionPaperEntity.ResPostBean(ZnbkPaperRdtzInfor.ResID,ZnbkPaperRdtzInfor.ResType,2,
                        UserInfo.StuId, UserInfo.SubjectId,ZSDCodeAll);

        SaveHeatPointExtensionPaperEntity saveHeatPointExtensionPaperEntity = new SaveHeatPointExtensionPaperEntity();
        saveHeatPointExtensionPaperEntity.SaveHeatPointExtensionPaperEntity("",resModelBean,UserInfo.SchoolId,resPostBean,
                ZnbkPaperRdtzInfor.Flag);

        api.setmSaveHeatPointExtensionPaperEntity(saveHeatPointExtensionPaperEntity);


        HttpManager httpManager = HttpManager.getInstance();
        httpManager.doHttpDeal(api);
    }

    @Override
    public void getMoreData() {
        getData(false);
    }

    @Override
    public void getDataByStatus(int position) {
        getData(false);
    }

    @Override
    public void getDataByQuesType(String quesTypeId) {

    }

    @Override
    public void getDataByQuesRank(int quesRankId) {

    }

}